<!-- 前台页面 -->
<template>
  <div>
    <div class="front-notice"><el-icon><Bell /></el-icon>公告：{{ data.top }}</div>
    <div class="front-header">
      <div class="front-header-left">
        <img :src="data.user.avatar" alt="" />
        <div class="title">项目前台</div>
      </div>

      <div class="front-header-center">
        <el-menu
          :default-active="router.currentRoute.value.path"
          router
          mode="horizontal"
        >
          <el-menu-item index="/front/home">首页</el-menu-item>
          <el-menu-item index="/front/person">个人中心</el-menu-item>
        </el-menu>
      </div>

      <div class="front-header-right">
        <!-- 判断用户是否登录 -->
        <div v-if="!data.user.id">
          <!-- 共用一个登录注册界面 -->
          <el-button @click="router.push('/login')">登录</el-button>
          <el-button @click="router.push('/register')">注册</el-button>
        </div>
        <div v-else>
          <el-dropdown style="cursor: pointer; height: 60px">
            <div style="display: flex; align-items: center">
              <img
                style="width: 40px; height: 40px; border-radius: 50%"
                :src="data.user.avatar"
                alt=""
              />
              <span style="margin-left: 5px">{{ data.user.name }}</span
              ><el-icon><arrow-down /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </div>
    <div class="main-body">
      <!-- 当前路由对应的视图内容 -->
      <RouterView @updateUser="updateUser" />
    </div>
  </div>
</template>

<script setup>
import router from "@/router/index.js";
import { reactive } from "vue";
import request from "@/utils/request.js";

//  定义一个变量装用户数据 data.user.id
const data = reactive({
  user: JSON.parse(localStorage.getItem("xm-user") || "{}"),
  noticeData: [],
  top: ''
});

// 更新用户数据
const updateUser = () => {
  data.user = JSON.parse(localStorage.getItem("xm-user") || "{}");
};

// 加载公告
const loadNotice = () => {
  request.get('/notice/selectAll').then(res => {
    data.noticeData = res.data
    let i = 0
    // 判断是否有数据
    if (data.noticeData && data.noticeData.length) {
      // 设置初始公告
      data.top = data.noticeData[0].content
      setInterval(() => {
        // 自动滚动
        data.top = data.noticeData[i].content
        i++
        // 如果到达数组长度 重置索引
        if (i === data.noticeData.length) {
          i = 0
        }
      }, 2500)
    }
  })
}
loadNotice()

const logout=()=>{
  router.push('/login')
  // 退出登录清除缓存
  localStorage.removeItem('xm-user')
}
</script>

<style scoped>
@import "../assets/css/front.css";


</style>
